package Java7_Concurrency_Cookbook.thread_synchronized_base.wait_notify;

import java.util.Date;
import java.util.LinkedList;
import java.util.List;

public class EventStorage {
    private int maxSize;
    private List<Date> storage;

    public EventStorage() {
        this.maxSize = 10;
        this.storage = new LinkedList<>();
    }

    public synchronized void set() throws InterruptedException {
        while (storage.size() == maxSize) {
            wait();
        }
        storage.add(new Date());
        System.out.println("Set: " + storage.size());
        notifyAll();
    }

    public synchronized void get() throws InterruptedException {
        while (storage.size() == 0) {
            wait();
        }
        Date d = ((LinkedList<Date>)storage).poll();
        System.out.printf("Get: %d: %s", storage.size(), d + "\n");
        notifyAll();
    }
}
